/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(function () {
    $("#template1 div[name='sel']").live("mouseenter",function () {
        $(this).addClass("heightlight2");
    });
    
    $("#template1 div[name='sel']").live("mouseleave",function () {
        $(this).removeClass("heightlight2");
    });

    $("#template1 div[name='sel']").live("click",function () {
        $("#template1 div[name='sel']").each(function () {
            $(this).removeClass("heightlight1");
            $(this).removeClass("heightlight2");
        });
        $(this).addClass("heightlight1");
    });
});

function PostAnswer()
{   
    var right=0;
    if($(".heightlight1").size()==1)
    {
        var right=  $(".heightlight1").eq(0).attr("tag");
    }
    else
    {
        $.miniAlert("请选择答案",2000);
        return false;
    }
    $("#btnsubmit1").attr('disabled','disabled');
    var options = {
        type: 'POST',
        url: 'QuickTestAction.php?v=set',
        data: {
            wordid:$("#wordid").val(),
            right:right
        },
        dataType: "json"
    };
    
    if(right==1)
    {
        $.miniAlert("正确",1000);      
    }
    else
    {
        $.miniAlert("错误",1000);
    }
    
    $.ajax(options).done(
        function (result) { 
            if(result.returnval!=1&&result.returnval!=0){
                $.miniAlert("出错");
            }
        })
    .fail(function () {
        $.miniAlert("异常");
    });
    Next();
    //拼接此次测试的单词，答案
    preword=$.trim($("#template1").find("div[tag='1']:eq(0)").html());
    preword=$.trim($("#template1 #divword span").html())+$.trim(preword.substr(3));
    $("#pre_word").html(preword);
    $("#pre_wordid").val($("#wordid").val());
}

function PostAnswer_write(){
    if($.trim($("#template2 #testword").val())=="")
    {
        return;
    }
    
    $("#btnsubmit2").attr('disabled','disabled');
    var right=0;
    if($.trim($("#template2 #testword").val())==$.trim($("#template2 #answer").val()))
    {
        $.miniAlert("正确",1000);
        right=1;
    }
    else if($.trim($("#template2 #answer_read").val())!=""
        &&$.trim($("#template2 #testword").val())==$.trim($("#template2 #answer_read").val())
        )
        {
        $.miniAlert("正确",1000);
        right=1;
    }
    else
    {
        $.miniAlert("错误："+$("#template2 #answer").val(),2000);
        right=0;
    }
    
    var options = {
        type: 'POST',
        url: 'QuickTestAction.php?v=set',
        data: {
            wordid:$("#wordid").val(),
            right:right
        },
        dataType: "json"
    };
    $.ajax(options).done( function (result) { 
        if(result.returnval!=1&&result.returnval!=0){
            $.miniAlert("出错");
        }
    }) .fail(function () {
        $.miniAlert("异常");
    });
    
    Next();
    //拼接此次测试的单词，答案
    preword=$.trim($("#template2 div #answer").val())+$.trim($("#template2 div #answer_read").val())+$.trim($("#template2 #testwordmeaning").html());
    $("#pre_word").html(preword);
    $("#pre_wordid").val($("#wordid").val());
}

function Enter_PostAnswer_write(event)
{
    if (event.keyCode == "13"&&$('#btnsubmit2').is(':disabled') == false) {
        PostAnswer_write();
    }
}

function Next()
{
    var options = {
        type: 'POST',
        url: 'QuickTestAction.php?v=getNext',
        dataType: "json"
    };
    $.ajax(options).done(function (result) { 
        if(result.finished==true)
        {
            $.miniAlert("测试完成！");
            $.modal.close(); // must call this!
            GetTestResult();
            return;
        }
        $("#wordid").val(result.wordid);
        if(result.template==1)
        {
            $("#template1").show();
            $("#template2").hide();
            $("#divword span:eq(0)").html(result.word);
            $("#divoptions").html(result.options);
        }
        else if(result.template==2)
        {
            $("#template1").hide();
            $("#template2").show();
            $("#template2 #testwordmeaning").html(result.species+result.meaning);
            $("#template2 #answer").val(result.word);
            $("#template2 #answer_read").val(result.read);
            $("#template2 #testword").val("");
        }
    }).always(function(){
        $("#btnsubmit1").removeAttr('disabled');
        $("#btnsubmit2").removeAttr('disabled');
       
    });
}

function showQuicktest()
{
    //取得要测试的课数
    var lessonid="";
    var bid="";
    bid=$("#simplemodal-container").find(".node:visible:eq(0)").attr("tag");
    $("#simplemodal-container").find(".node:visible:eq(0)").find("input:checked").each(function(){
        lessonid+=$(this).attr("tag")+",";
    });
    lessonid = lessonid.replace(/,$/, '');
    if($.trim(lessonid)=="")
    {
        $.miniAlert("请选择课程！");
        return false;
    }
    var accordingby=$('#simplemodal-container input[name=accordingby]:checked').attr('value');
    var quizType=$('#simplemodal-container input[name=quizType]:checked').attr('value');
    var geturl="/RecJP/QuickTest.php?lessonid="+lessonid+"&bid="+bid+"&accordingby="+accordingby+"&quizType="+quizType;
    $.get(geturl,function(result){
        $.modal.close();
        $(result).modal({
            autoResize:true, 
            maxWidth:"400",
            maxHeight:"300",
            onClose:function(dialog){
                dialog.data.fadeOut('fast', function () {
                    dialog.container.slideUp('fast', function () {
                        dialog.overlay.fadeOut('fast', function () {
                            $.modal.close(); // must call this!
                            GetTestResult();
                        });
                    });
                });
            }
        });
    });
}

function GetTestResult()
{
    var options = {
        type: 'POST',
        url: 'QuickTestAction.php?v=getTestResult',
        dataType: "json"
    };
    $.ajax(options).done(function (result) {
        str="测试题数目："+result.sum+"<br/>";
        str+="正确："+result.rightcount+"<br/>";
        str+="正确率："+result.rate+"%<br/>";
        $.miniAlert(str);
    });
}

function Unknow()
{
    if($("#template1").is(":hidden")==false&&$("#template2").is(":hidden")==true)
    {
        var answer= $("#divoptions").find("div[tag='1']").html();
        $.miniAlert(answer,2000);
    }
    else if($("#template1").is(":hidden")==true&&$("#template2").is(":hidden")==false)
    {
        $.miniAlert($("#template2 #answer").val(),3000); 
    }
    Next();
}

function Skip()
{
    var wordid=$("#wordid").val();
    $.post( 'QuickTestAction.php?v=skip',{
        wordid:wordid
    });
    Next(); 
}

function ReportWrongWord(id)
{
    strUrl="ReportWrongWord.php?wid="+id;
    window.open(strUrl);
}